package com.kfm.jdbc.day0325;

// DBUtils

import javax.sound.sampled.ReverbType;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test<T> {

    private String url = "jdbc:mysql://localhost:3306/store";
    private String user = "root";
    private String password = "";

    private Connection connection;

    public Connection getConnection() throws SQLException, ClassNotFoundException {
        if (connection == null || connection.isClosed()) {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, user, password);
        }
        return connection;
    }


    public void close() throws SQLException {
        if (connection != null && !connection.isClosed()) {
            connection.close();
            connection = null;
        }
    }

    public int executeUpdate(String sql, Object... values) throws SQLException, ClassNotFoundException {
        // 获取连接
        getConnection();
        // 获取执行sql的对象
        PreparedStatement statement = connection.prepareStatement(sql);
        // 对象.方法（） 方法（）
        //statement.setObject(1, values[0]);
        //statement.setObject(2, values[1]);
        // 替换占位符
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                statement.setObject(i + 1, values[0]);
            }
        }
        // 执行sql
        int rows = statement.executeUpdate();
        // 返回
        return rows;
    }

    public List<Map<String, Object>> executeQuery(String sql, Object... values) throws SQLException, ClassNotFoundException {
        getConnection();
        PreparedStatement statement = connection.prepareStatement(sql);
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                statement.setObject(i + 1, values[0]);
            }
        }
        ResultSet resultSet = statement.executeQuery();
        // 解析resultSet
        ResultSetMetaData metaData = resultSet.getMetaData();
        List<Map<String, Object>> list = new ArrayList<>();
        while (resultSet.next()) {
            Map<String, Object> map = new HashMap<>();
            for (int i = 1; i <= metaData.getColumnCount(); i++) {
                String key = metaData.getColumnName(i);
                Object value = resultSet.getObject(i);
                map.put(key, value);
            }
            list.add(map);
        }
        return list;
    }

    public T selectOne() {
        return null;
    }

    public List<T> executeQuery1(String sql,Recover<T> recover, Object... values) throws SQLException, ClassNotFoundException {
        getConnection();
        PreparedStatement statement = connection.prepareStatement(sql);
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                statement.setObject(i + 1, values[0]);
            }
        }
        ResultSet resultSet = statement.executeQuery();
        return recover.recover(resultSet);
        // 解析resultSet
        //ResultSetMetaData metaData = resultSet.getMetaData();
        //List<Goods> list = new ArrayList<>();
        //while (resultSet.next()) {
        //    // 封装成Goods对象
        //    Map<String, Object> map = new HashMap<>();
        //    Goods goods = new Goods();
        //    //for (int i = 1; i <= metaData.getColumnCount(); i++) {
        //    //    //String key = metaData.getColumnName(i);
        //    //    Object value = resultSet.getObject(i);
        //    //
        //    //    //map.put(key, value);
        //    //}
        //    goods.setGoods_no(resultSet.getInt(1));
        //    goods.setGoods_name(resultSet.getString(2));
        //    goods.setCost(resultSet.getDouble(3));
        //    goods.setPrice(resultSet.getDouble(4));
        //    goods.setCount(resultSet.getInt(5));
        //    goods.setCreateTime(resultSet.getDate(6));
        //    goods.setCategoryNo(resultSet.getInt(7));
        //    list.add(goods);
        //}
        //return list;
    }
}
